﻿function SetupControls() {
    $("[propertyname]").change(function() { UpdateField(this, "Sede"); });
    $(".date").datepicker();
     
    if (action != "search") {
        if (action == "create" || action == "edit"){            
            AddValidators();
            SetValidation("Sede");
            $("[propertyname]").removeAttr("disabled");
            $("#Sede_CodSede").attr("disabled", "disabled");
            $("#btnBuscar").hide();
            $('.ActionLink').bind('click', true);
        }
        if (action == "query") {
            $("[propertyname]").attr("disabled", "disabled");
            $('.ActionLink').hide();
            $("#AddPersonal").attr("disabled", "disabled");
            $("#Sede_CodSede").removeAttr("disabled");            
            $("#btnBuscar").show();
            $("#btnBuscar").click(function() {
                DoSearch();
            });
        }
    }
    else {
        $("[propertyname]").attr("disabled", "disabled");
        $('.ActionLink').hide();
        $("#AddPersonal").attr("disabled", "disabled");
        $("#Sede_CodSede").removeAttr("disabled");        
        $("#btnBuscar").show();
        $("#btnBuscar").click(function() {
            DoSearch();
        });
        
    }
}

function SetupActions() {
    $("#btnNuevo").click(function() {
        apprise("¿Desea crear una Sede?", { 'confirm': true }, function(r) {
            if (r) {
                window.location = "/sede/create";
            }
        });
    });

    $("#btnEditar").click(function() {
        var cod = $('#Sede_CodSede').val();
        apprise("¿Desea modificar la Sede seleccionada?", { 'confirm': true }, function(r) {
            if (r) {
                window.location = "/sede/edit?CodSede=" + cod;
            }
        });
    });    

    $("#btnGrabar").click(function() {
        if ($('img.validation:visible').length) {
            apprise('Por favor corregir todos los errores de validación.');
            return;
        }
    
        if (action == "create" || action == "edit")
              Save();
    });

    $("#btnCancelar").click(function() {
        apprise("¿Desea cancelar sus cambios?", { 'confirm': true }, function(r) {
            if (r) {
                window.location = "/sede/search";
            }
        });
    });
}

function DoSearch() {
    var cod = $('#Sede_CodSede').val();
    window.location = "/sede/DoSearch?codigo=" + cod;
}

function Save() {
    $.blockUI({ message: "Procesando" });	
    var data = { id: guid, action: action };
    $.ajax({
        type: "POST",
        url: GetControllerUrl("Save", "sede"),
        data: data,
        success: function(result) {
            apprise(result.message, {}, function(r) {
                $.unblockUI();
                window.location = "/sede/search";
            });
        }
    });
}

function AddOrEditPersonal(action) {
    $.blockUI({ message: "Procesando" });
    var data = { id: guid, personalGuid: personalGuid, action: action };
    $.ajax({
        type: "POST",
        url: GetControllerUrl("AddOrEditPersonal", "Sede"),
        data: data,
        success: function(result) {
            $.unblockUI();
            LoadAddListaPersonal();
        }
    });
}

function LoadAddListaPersonal() {
    var data = { id: guid };
    $.ajax({
        type: "POST",
        url: window.GetControllerUrl("GetListaPersonal", "Sede"),
        data: data,
        success: function(data) {
            $("#ListaPersonalDiv").html(data);
        }
    });
}

function LoadAddOrEditPersonal(personalGuid) {
    var data = { id: guid,
        personalGuid: personalGuid
    };
    $.ajax({
        type: "POST",
        data: data,
        url: window.GetControllerUrl("LoadAddOrEditPersonal", "Sede"),       
        success: function (data) {
            $("#PersonalPopupDiv").html(data);            
        }
    });
}

function DeletePersonal(personalGuid) {
    var data = { id: guid,
        personalGuid: personalGuid
    };
    $.ajax({
        type: "POST",
        data: data,
        url: window.GetControllerUrl("DeletePersonal", "Sede"),
        success: function(data) {            
            $("#ListaPersonalDiv").html(data);
        }
    });
}

function SetupDropDowns(){
    $("#Ubigeo_CodDpto").change(function() {

        var url = "/Sede/SeleccionarDepartamentoProvincia";
        var ddlsource = "#Ubigeo_CodDpto";
        var ddltarget = "#Ubigeo_CodProv";
        var ddlcboCodDist = "#Ubigeo_CodDist";

        $.getJSON(url, { Sel_Dpto: $(ddlsource).val() }, function(data) {
            $(ddltarget).empty();
            $(ddlcboCodDist).empty();
            $(ddltarget).append("<option  value='Seleccione'>Seleccione...</option>");
            $(ddltarget).change();
            $(ddlcboCodDist).append("<option  value='Seleccione'>Seleccione...</option>");
            $.each(data, function(index, optionData) {
                $(ddltarget).append("<option  value='" + optionData.CodProv + "'>" + optionData.DescUbigeo + "</option>");
            });
        });
    });

    $("#Ubigeo_CodProv").change(function() {

        var url = "/Sede/SeleccionarDistritoProvincia";
        var ddlcboCodDpto = "#Ubigeo_CodDpto";
        var ddlcboCodProv = "#Ubigeo_CodProv";
        var ddlcboCodDist = "#Ubigeo_CodDist";

        $.getJSON(url, { Sel_Prov: $(ddlcboCodProv).val(), Sel_Dpto: $(ddlcboCodDpto).val() }, function(data) {
            $(ddlcboCodDist).empty();
            $(ddlcboCodDist).append("<option  value='Seleccione'>Seleccione...</option>");
            $(ddlcboCodDist).change();
            $.each(data, function(index, optionData) {
                $(ddlcboCodDist).append("<option  value='" + optionData.CodDist + "'>" + optionData.DescUbigeo + "</option>");
            });
        });
    });


}